using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;

namespace ARF.Web.Controls
{
    public class ARFPanel : Panel
    {
        private ArfCache _cacheObject;

        public ArfCache Cache
        {
            get { if (_cacheObject == null) _cacheObject = new ArfCache(CacheKey); return _cacheObject; }
            set { _cacheObject = value; }
        }

        protected virtual string CacheKey
        {
            get { return UniqueID; }
        }

        protected virtual bool CustomRendering
        {
            get { return false; }
        }

        protected virtual string ModifyRenderedHtml(string html)
        {
            return html;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (CustomRendering)
            {
                if (Cache.IsCached)
                {
                    writer.Write(Cache.Value);
                    return;
                }

                // TODO: Put some caching here
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                base.RenderChildren(htw);

                sw.Close();

                string sHTML = ModifyRenderedHtml(sw.ToString());

                writer.Write(sHTML);

                sw.Dispose();
                htw.Dispose();

                if (Cache.Enabled) Cache.Value = sHTML;
            }
            else
                base.RenderChildren(writer);
        }
    }
}
